package com.qixia.sportsupload.util;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 *
 * @author Wayne
 * @date 2019/4/30
 */
@Component
public class RedisUtil {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 向Redis中存值，永久有效
     */
    public void set(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    /**
     * 根据传入Key获取指定Value
     */
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 向Redis中存值，永久有效
     */
    public void setHash(String key, String hashKey, String value) {
        stringRedisTemplate.opsForHash().put(key, hashKey, value);
    }

    public void delHash(String key, String hashKey) {
        stringRedisTemplate.opsForHash().delete(key, hashKey);
    }

    /**
     * 根据传入Key获取指定Value
     */
    public String getHash(String key, String hashKey) {
        return stringRedisTemplate.opsForHash().get(key, hashKey) == null ? "" : (String) stringRedisTemplate.opsForHash().get(key, hashKey);
    }

    /**
     * 删除指定Key-Value
     */
    public boolean delete(String key) {
        return Boolean.TRUE.equals(stringRedisTemplate.delete(key));
    }

    /**
     * 有过期时间普通添加
     *
     * @param key    键
     * @param value  值
     * @param expire 过期时间
     */
    public void setEx(String key, String value, int expire) {
        stringRedisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
    }

    public void batchDelete(String key) {
        Set<String> keys = stringRedisTemplate.keys(key + "*");
        if (!CollectionUtils.isEmpty(keys)) {
            stringRedisTemplate.delete(keys);
        }
    }

    public boolean hasKey(String key) {
        return stringRedisTemplate.hasKey(key);
    }


    private static final String REDIS_KEY_PREFIX = "ORDER_NO_";
    private static final int ORDER_NO_MAX_LENGTH = 6;

    public String getOrderNo(String code) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String key = REDIS_KEY_PREFIX + code + "_" + sdf.format(new Date());
        long value = 1;
        String cache = stringRedisTemplate.opsForValue().get(key);
        if (StringUtils.hasLength(cache)) {
            value = Long.parseLong(cache);
        } else {
            stringRedisTemplate.opsForValue().set(key, String.valueOf(value), 24, TimeUnit.HOURS);
        }
        String valueStr = String.valueOf(value);
        int valueStrLength = valueStr.length();
        if (valueStrLength < ORDER_NO_MAX_LENGTH) {
            for (int i = 0; i < (ORDER_NO_MAX_LENGTH - valueStrLength); i++) {
                valueStr = "0" + valueStr;
            }
        }
        return sdf.format(new Date()) + valueStr;
    }
}